import {Injectable} from '@angular/core';
import {ActivatedRouteSnapshot, Resolve, Router, RouterStateSnapshot} from '@angular/router';
import {Chero} from '../../model/chero';
import {Observable} from 'rxjs/Observable';
import {CheroService} from '../cheroService/chero.service';

@Injectable()
export class CherodetailService implements Resolve<Chero> {


  constructor(private cheroService: CheroService, private router: Router) {


  }

  resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<Chero> | Promise<Chero> | Chero {

    const id = route.params['id'];
    return this.cheroService.getCHero(id).then(res => {

      if (res) {
        return res;
      } else {
        this.router.navigate(['./crisis-center'])

        return null;
      }
    });
  }


}
